package com.aubrey.threadDemo.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LockDemo {
	
	public static void main(String[] args) {
		LockOperation lo = new LockOperation();
		for (int i = 0; i < 5; i++) {
			new Thread(()->{
				lo.increment();
			}).start();
		}

	}

}

class LockOperation{
	private int a = 0;
	Lock lock = new ReentrantLock();
	
	public void increment() {
		lock.lock();
		for (int j = 0; j < 10; j++) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(++a);
		}
		lock.unlock();
	}
}
